package common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class Statistics 
{
	ArrayList<String> data;
	double[] temp;
    double size;    
    
    /**
     * Constructor
     * @param data
     */
    public Statistics(ArrayList<String> data) 
    {
        this.data = data;
        size = data.size();
    } 
    
    public Statistics(double[] data) 
    {
        this.temp = data;
        size = data.length;
    }
    
    public double getMean()
    {
        double sum = 0.0;
        for(int i=0;i<size;i++)
            sum += (double)(Integer.parseInt(data.get(i)));
            return sum/size;
    }
    
    
        public double getVariance()
        {
            double mean = getMean();
            double temp = 0;
            for(int i=0;i<size;i++)
                temp += (mean-(double)(Integer.parseInt(data.get(i))))*(mean-(double)(Integer.parseInt(data.get(i))));
                return temp/size;
        }

        public double getStdDev()
        {
            return Math.sqrt(getVariance());
        }
        
        /**
         * Function calculate median for statistic use
         * @return median
         */
        public double median() 
        {
               temp= new double[data.size()];
               for(int i=0;i<size;i++)
               		{
            	   	temp[i]=(double)(Integer.parseInt(data.get(i)));
               		}
               double[] b = new double[temp.length];
               System.arraycopy(temp, 0, b, 0, b.length);
               Arrays.sort(b);

               if (data.size() % 2 == 0) 
               {
                  return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
               } 
               else 
               {
                  return b[b.length / 2];
               }
        }
        
 public static double analyzer(){
    Random r = new Random();
    double value = 3 + (100 - 1)*r.nextDouble();
    return value;   		
  }
}
